/**
 * 
 */
package com.cudex.negocio.daos;

import java.util.ArrayList;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

import org.apache.log4j.Logger;
import org.hibernate.exception.ConstraintViolationException;

import com.cudex.negocio.beans.Clientes;
import com.cudex.negocio.beans.Facturas;

/**
 * @author Nicolas Bordenave
 * @date 04/12/2011
 * @version 1.0
 */

@Stateless
public class FacturaDAO implements IFacturaDAO {

	private static final Logger log = Logger.getLogger(FacturaDAO.class
			.getName());

	private static final long serialVersionUID = 1740683200836093140L;

	@PersistenceContext(unitName = "JPA_UNIT_CUDEX")
	EntityManager em;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cudex.negocio.daos.IFacturaDAO#altaFactura(com.cudex.negocio.beans
	 * .Facturas)
	 */
	@Override
	public Boolean altaFactura(Facturas f) throws Exception {
		Boolean result = false;
		log.info("altaFactura()");
		try {
			em.persist(f);
			result = true;
		} catch (ConstraintViolationException e) {
			// log.error("salvarTipo : ConstraintViolationException");
			throw e;
		} catch (PersistenceException e) {
			// log.error("salvarTipo : PersistenceException");
			throw e;
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cudex.negocio.daos.IFacturaDAO#modificarFactura(com.cudex.negocio
	 * .beans.Facturas)
	 */
	@Override
	public Facturas modificarFactura(Facturas f) throws Exception {
		log.info("modificarFactura()");
		Facturas objTmp = null;
		try {
			objTmp = em.merge(f);
			em.flush();

		} catch (ConstraintViolationException e) {
			// log.error("modificarTipo : ConstraintViolationException");
			throw e;
		} catch (PersistenceException e) {
			// log.error("modificarTipo : PersistenceException");
			throw e;
		} catch (Exception e) {
			throw e;
		}
		return objTmp;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cudex.negocio.daos.IFacturaDAO#eliminarFactura(com.cudex.negocio.
	 * beans.Facturas)
	 */
	@Override
	public Boolean eliminarFactura(Facturas f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.cudex.negocio.daos.IFacturaDAO#buscarFacturas()
	 */
	@Override
	public Facturas buscarFacturas() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cudex.negocio.daos.IFacturaDAO#buscarFactura(com.cudex.negocio.beans
	 * .Facturas)
	 */
	@Override
	public Facturas buscarFactura(Facturas f) throws Exception {
		log.info("buscarFactura()");
		try {
			Facturas objTmp = (Facturas) em
					.createNamedQuery("buscarFactura", Facturas.class)
					.setParameter("Fac", f).getSingleResult();
			return objTmp;
		} catch (Exception e) {
			log.error(e.getCause());
			// e.printStackTrace();
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.cudex.negocio.daos.IFacturaDAO#buscarFacturasCliente(com.cudex.negocio
	 * .beans.Clientes)
	 */
	@Override
	public Facturas buscarFacturaCliente(Clientes c) throws Exception {
		log.info("buscarFacturaCliente()");
		try {
			Facturas objTmp = (Facturas) em
					.createNamedQuery("buscarFacturaCliente", Facturas.class)
					.setParameter("Cli", c).getSingleResult();
			return objTmp;
		} catch (Exception e) {
			log.error(e.getCause());
			// e.printStackTrace();
			throw e;
		}
	}

	@Override
	public ArrayList<Facturas> buscarFacturasCliente(Clientes c)
			throws Exception {
		log.info("buscarFacturasCliente()");
		try {
			ArrayList<Facturas> facts = (ArrayList<Facturas>) em
					.createNamedQuery("buscarFacturaCliente", Facturas.class)
					.setParameter("Cli", c).getResultList();
			return facts;
		} catch (Exception e) {
			log.error(e.getCause());
			// e.printStackTrace();
			throw e;
		}
	}

}
